﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Farmacia.Entities;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;

using System.Data;
using System.Data.Common;

using Microsoft.Practices.Unity;


namespace Farmacia.DataAccess
{
    public class ClienteDAO : MasterDAEL, iClienteDAO
    {

        public List<Cliente> GetAllFromCliente()
        {

            var query = DataBase.ExecuteSprocAccessor<Cliente>("GetAllFromClientes");

            return query.ToList();

        }

        public Cliente GetFromClienteByRucDni(string rucDni)
        {
            var query = DataBase.ExecuteSprocAccessor<Cliente>("GetFromClienteByRucDni", rucDni);

            return query.SingleOrDefault();
        }

        public void RegistrarCliente(Cliente cliente)
        {
            DbCommand command = DataBase.GetStoredProcCommand("RegistrarCliente");
            //Para guiarse de que datos o campos van en el agregar
            /*  @idCliente int out,
                @rucDni varchar(20),
                @razonSocial varchar(200),
                @direccion varchar(200) = null*/
            DataBase.AddInParameter(command, "idCliente", DbType.Int32, cliente.IdCliente);
            DataBase.AddInParameter(command, "rucDni", DbType.String, cliente.RucDni);
            DataBase.AddInParameter(command, "razonSocial", DbType.String, cliente.RazonSocial);
            DataBase.AddInParameter(command, "direccion", DbType.String, cliente.Direccion);
            DataBase.AddInParameter(command, "telefono", DbType.String, cliente.Telefono);
            DataBase.ExecuteNonQuery(command);
        }

        public List<Cliente> GetFromClienteByRazonSocialAndRucDni(string criterio)
        {
            var query = DataBase.ExecuteSprocAccessor<Cliente>("GetFromClienteByRazonSocialAndRucDni", criterio);

            return query.ToList();
        }


        public Cliente GetFromClienteById(Int32 id)
        {
            var query = DataBase.ExecuteSprocAccessor<Cliente>("GetFromClienteById", id);

            return query.SingleOrDefault(); 
        }


        public void DeleteCliente(Int32 id)
        {
            try
            {
                DbCommand command = DataBase.GetStoredProcCommand("EliminarCliente");
                DataBase.AddInParameter(command, "idCliente", DbType.Int32, id);
                DataBase.ExecuteNonQuery(command);
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                if (ex.Number == 547)
                {
                    var cliente = GetFromClienteById(id);

                    throw new ApplicationException(string.Format("No puede Eliminar el Cliente: {0} porque tiene información asociada", cliente.RazonSocial.ToUpper()));
                }
                else
                {
                    throw new ApplicationException(string.Format("Error: {0} - Mensaje: {1}", ex.Number, ex.Message));
                }

            }
            
        }
    }
}
